﻿Public Class OPERATEGROUP
    Dim connect As CONDB = CONDB.NewConnection()
    Dim s As FILTERCLASS
    Dim Sql As String
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub OPERATEGROUP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridMasgrpoper()

        DesignForm(Me)
        DesignGrid(MASGRPOPERGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub
    Private Sub SelectDatagridMasgrpoper()
        Dim objSelectDataMasgrpoper As New MASGRPOPER
        Dim MASGRPOPER As DataTable
        MASGRPOPER = connect.GetTable(objSelectDataMasgrpoper.SelectMasgrpoper_)
        MASGRPOPERGrid.PrimaryGrid.DataSource = MASGRPOPER
    End Sub
    Private Sub cleardata()
        GRPNAMETxt3.Text = ""
        CODETxt3.Text = ""
        MasgrpoperStatusChk.Checked = False
        GRPIDTxt.Text = ""
    End Sub
    Private Sub addMasgrpoper_Click(sender As Object, e As EventArgs) Handles addMasgrpoper.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If GRPNAMETxt3.Text.ToString.Length > 0 Then
                    Dim objInsertData As New MASGRPOPER
                    objInsertData.GRPNAME_ = GRPNAMETxt3.Text
                    objInsertData.CODE_ = CODETxt3.Text
                    objInsertData.STATUS_ = MasgrpoperStatusChk.Checked
                    objInsertData.insertMasgrpoperData()

                    SelectDatagridMasgrpoper()

                    cleardata()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasgrpoper_Click(sender As Object, e As EventArgs) Handles editMasgrpoper.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If GRPIDTxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objeditData As New MASGRPOPER
                        objeditData.GRPNAME_ = GRPNAMETxt3.Text
                        objeditData.CODE_ = CODETxt3.Text
                        objeditData.GRPID_ = GRPIDTxt.Text
                        objeditData.STATUS_ = MasgrpoperStatusChk.Checked
                        objeditData.editMasgrpoperData()

                        SelectDatagridMasgrpoper()

                        cleardata()
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                addMasgrpoper.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub MASGRPOPERGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASGRPOPERGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("grpid").Value() IsNot DBNull.Value Then
            GRPIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("grpid").Value()
        Else
            GRPIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่ม").Value() IsNot DBNull.Value Then
            CODETxt3.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่ม").Value()
        Else
            CODETxt3.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อกลุ่มหัตถการ").Value() IsNot DBNull.Value Then
            GRPNAMETxt3.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อกลุ่มหัตถการ").Value()
        Else
            GRPNAMETxt3.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            MasgrpoperStatusChk.Checked = True
        Else
            MasgrpoperStatusChk.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masgrpoper SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `grpid` = '" & GRPIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        addMasgrpoper.Enabled = False
    End Sub
End Class